/**
 * jims
 */
package com.jims.master.modules.reg.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.reg.entity.ClinicIndex;
import com.jims.master.modules.reg.entity.ClinicSchedule;
import com.jims.master.modules.reg.service.ClinicIndexService;
import com.jims.master.modules.reg.service.ClinicScheduleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 出诊安排Controller
 *
 * @author CHENXY
 * @version 2017-02-27
 */
@Controller
@RequestMapping(value = "${adminPath}/registration/clinicSchedule")
public class ClinicScheduleController extends BaseController {

    @Autowired
    private ClinicScheduleService clinicScheduleService;

    @Autowired
    private ClinicIndexService clinicIndexService;

    @ModelAttribute
    public ClinicSchedule get(@RequestParam(required = false) String id) {
        ClinicSchedule entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = clinicScheduleService.get(id);
        }
        if (entity == null) {
            entity = new ClinicSchedule();
        }
        return entity;
    }

    /**
     * 列表
     *
     * @param clinicSchedule
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ClinicSchedule clinicSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (clinicSchedule.getClinicIndexId() != null && !"".equals(clinicSchedule.getClinicIndexId())) {
            model.addAttribute("clinicIndexId", clinicSchedule.getClinicIndexId());
        }
        ClinicIndex clinicIndex = new ClinicIndex();
        clinicIndex.setName(clinicSchedule.getClinicIndexName());
        Page<ClinicIndex> page = clinicIndexService.findPage(new Page<ClinicIndex>(request, response), clinicIndex);
        model.addAttribute("page", page);
        model.addAttribute("clinicIndexName",clinicSchedule.getClinicIndexName());
        return "modules/reg/clinicScheduleIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ClinicSchedule clinicSchedule, Model model) {
        model.addAttribute("clinicSchedule", clinicSchedule);
        return "modules/reg/clinicScheduleForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ClinicSchedule clinicSchedule, Model model) {
        StringData data = new StringData();
        int i = clinicScheduleService.save(clinicSchedule);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存出诊安排成功");
        } else {
            data.setCode("error");
            data.setData("保存出诊安排失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ClinicSchedule clinicSchedule) {
        StringData data = new StringData();
        int i = clinicScheduleService.delete(clinicSchedule);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除出诊安排成功");
        } else {
            data.setCode("error");
            data.setData("删除出诊安排失败");
        }
        return data;
    }

    /**
     * getByClinicIndexId 通过号别Id 查找出诊安排
     *
     * @param clinicSchedule
     * @param printWriter
     * @return
     * @Author chenxy
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "getByClinicIndexId", method = RequestMethod.POST)
    public List<ClinicSchedule> getByClinicIndexId(@ModelAttribute ClinicSchedule clinicSchedule, PrintWriter printWriter) {
        if (clinicSchedule.getClinicIndexId() == null || "".equals(clinicSchedule.getClinicIndexId())) {
            throw new IllegalArgumentException("号别ID为空！");
        }
        clinicSchedule.updateOrgId();
        List<ClinicSchedule> list = this.clinicScheduleService.getByClinicIndexId(clinicSchedule.getClinicIndexId());
        List<ClinicSchedule> resultValue = new ArrayList<ClinicSchedule>();
        StringBuffer s = new StringBuffer();
        for (ClinicSchedule c : list) {
            Integer index = this.findIndex(c);
            c.setIndex(index);
        }
        return list;
    }

    /**
     * 计算此个出诊安排在页面显示的索引
     * 通过dayOfWeek 与timeDesc 确定index
     *
     * @param clinicSchedule
     * @return
     * @Author chenxy
     */
    private Integer findIndex(ClinicSchedule clinicSchedule) {
        Integer timeDesc = Integer.parseInt(clinicSchedule.getTimeDesc());
        Integer dayOfWeek = Integer.parseInt(clinicSchedule.getDayOfWeek());
        int a = (timeDesc - 1) * 7 + dayOfWeek;
        return a;
    }

}


